<template>
  <c-form-dialog
    label-width="200px"
    title="项目受理"
    ref="form"
    :form-items="formItems"
    :form-data="formData"
    :table-name="putTableName"
    :before-save="handleBeforeSave"
    @success="handleSuccess"
    @dismiss="handleDismiss"
    :submit="false"
    :dismiss="false"
    :table-id="tableId"
    :visible.sync="innerVisible"
  >
    <el-form-item>
      <el-button
        @click="handleApprovalPass"
        class="btn-long"
        type="success"
        :loading="approvalLoading"
      >
        确认受理
      </el-button>
      <el-button
        @click="handleApprovalReject"
        v-if="confuse"
        class="btn-long"
        type="danger"
        :loading="approvalLoading"
      >
        拒绝受理
      </el-button>
      <el-button @click="handleDismiss" v-else class="btn-long" :loading="approvalLoading">
        取消
      </el-button>
    </el-form-item>
  </c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectUser from '@/bank/components/select/SelectUser'
import SelectProduct from '@/bank/views/limit/SelectProduct'
import SelectAddress from '@/components/select/SelectAddress'
import SelectCredit from '@/bank/views/business/library/SelectCredit'
export const enumLimitType = {
  0: '存量业务',
  1: '增量业务',
}
export const enumMissionType = {
  0: '常规28业务',
  1: '便捷28业务',
}
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: { type: Object, default: () => ({}) },
    confuse: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      innerVisible: false,
      putTableName: 'customer_related',
      staff: {},
      formItems: [
        {
          tag: 'input',
          itemAttrs: { label: '贷款客户' },
          attrs: { key: 'customer_name', value: '', disabled: true },
        },
        {
          tag: 'input',
          itemAttrs: { label: '申请金额' },
          attrs: { key: 'apply_funds', value: '', disabled: true },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'input',
          itemAttrs: { label: '贷款年限' },
          attrs: { key: 'load_years', value: '', disabled: true },
          tagSlots: [{ name: 'append', slot: '年' }],
        },
        {
          tag: SelectProduct,
          itemAttrs: {
            label: '担保产品',
            rules: [{ required: true, message: '请选择担保产品', trigger: 'change' }],
          },
          attrs: {
            key: 'project_type_id',
            value: '',
            class: 'form-control-ml',
            channelType: '',
            filterHistory: true,
          },
          events: {
            select: (model, name, product) => {
              model.project_type_name = name || ''
              model.gua_type = product.gua_type || ''
              model.channel_type = product.channel_type || ''
            },
          },
          ifRender: () => !this.formData.project_type_id,
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '担保产品',
          },
          attrs: {
            key: 'project_type_name',
            disabled: true,
          },
          ifRender: () => this.formData.project_type_id,
        },
        {
          tag: SelectCredit,
          itemAttrs: {
            label: '担保机构',
            rules: [{ required: true, message: '担保机构', trigger: 'change' }],
          },
          attrs: {
            key: 'service_id',
            value: '',
            class: 'form-control-ml',
            placeholder: '担保机构',
            clearable: true,
          },
          events: {
            change: (model, value, credit) => {
              model.service_name = credit.agency_name || ''
            },
          },
          ifRender: model => +this.formData.is_approval === 0 && +model.gua_type !== 3,
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '担保机构',
          },
          attrs: { key: 'service_name', value: '', disabled: true },
          ifShow: () => +this.formData.is_approval > 0,
        },
        {
          tag: SelectAddress,
          itemAttrs: {
            label: '经营地址',
            rules: { required: true, message: '请选择经营地址', trigger: 'change' },
          },
          attrs: {
            key: 'address_code',
            value: '',
            placeholder: '请选择经营地址',
            class: 'form-control-ml',
            autoTop: true,
          },
          ifRender: () => +this.formData.is_approval === 0,
        },
        {
          tag: 'input',
          itemAttrs: { label: '详细地址' },
          attrs: { key: 'address_detail', value: '', placeholder: '请输入详细地址（非必填）' },
          ifRender: () => +this.formData.is_approval === 0,
        },
        {
          tag: 'input',
          attrs: { key: 'project_type_name' },
          ifShow: false,
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '业务通道',
            rules: [{ required: true, message: '请选择业务通道', trigger: 'change' }],
          },
          attrs: {
            clearable: false,
            key: 'gua_type',
            options: { 3: '限率业务', 4: '28业务' },
            disabled: true,
          },
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '业务模式',
            rules: [{ required: true, message: '请选择业务模式', trigger: 'change' }],
          },
          attrs: { clearable: false, key: 'channel_type', options: enumLimitType, disabled: true },
          getAttrs: model => {
            return { options: +model.gua_type === 3 ? enumLimitType : enumMissionType }
          },
        },
        {
          tag: SelectUser,
          itemAttrs: {
            label: '项目经理',
            rules: [{ required: true, message: '请选择项目经理', trigger: 'change' }],
          },
          attrs: {
            key: 'bank_user_id',
            class: 'form-control-ml',
            value: '',
            includeJob: ['business_manager'],
          },
          events: {
            select: (model, staff) => {
              model.bank_user_name = staff.realName
              model.bank_user_phone = staff.phoneNumber
            },
          },
        },
        {
          tag: 'input',
          attrs: { key: 'bank_user_name', value: '' },
          ifShow: false,
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系电话',
            rules: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
          },
          attrs: { key: 'bank_user_phone', value: '', disabled: true },
        },
      ],
      approvalLoading: false,
    }
  },
  computed: {
    ...mapGetters(['clientId', 'org', 'user']),
    bankType() {
      return this.user.clientOrgClassType
    },
    bankName() {
      return this.org.orgName
    },
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  methods: {
    handleApprovalPass() {
      this.$refs.form.submit()
    },
    async handleApprovalReject() {
      if (!this.tableId) {
        return
      }
      this.$prompt('请输入拒绝理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: value => (!value ? '请输入不通过原因' : true),
      })
        .then(({ value }) => {
          this.approvalLoading = true
          this.$api
            .updateData(this.$serviceName, 'customer_related', this.tableId, {
              confuse_status: 3,
              confuse_time: this.$utils.dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'),
              confuse_person: this.user.realName,
              confuse_des: value,
              claim_time: this.$utils.dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'),
              claim_status: 2,
              bank_id: this.clientId,
              bank_code: this.org.orgCode,
              bank_name: this.org.orgName,
              claim_des: value,
              bank_user_id: this.user.userId,
              bank_user_name: this.user.realName,
              bank_user_phone: this.user.phoneNumber,
            })
            .then(() => {
              this.approvalLoading = false
              this.handleDismiss()
              this.$message.success('项目已拒绝')
            })
            .catch(() => this.$message.error(`项目拒绝失败，请重试`))
        })
        .catch(() => {})
    },
    handleDismiss() {
      this.innerVisible = false
      this.$emit('update:visible', false)
    },
    handleSuccess() {
      this.$emit('success', true)
    },
    handleBeforeSave(model) {
      model.claim_status = 1
      model.claim_time = this.$utils.dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss')
      if (+model.gua_type === 4 && +model.channel_type === 0) {
        model.recommend_status = 1
      }
      if (!model.service_id) {
        model.service_name = ''
      }
      model.bank_id = this.clientId
      model.bank_name = this.bankName
      model.bank_type = this.bankType
      return model
    },
  },
}
</script>
<style lang="scss" scoped></style>
